<%- include('../layout/header.ejs') %>

<!-- <link rel="stylesheet" href="../../node_modules/mditor/dist/css/mditor.min.css">
<script src="../../node_modules/mditor/dist/js/mditor.min.js"></script> -->

  <link rel="stylesheet" href="/node_modules/mditor/dist/css/mditor.min.css">
  <script src="/node_modules/mditor/dist/js/mditor.min.js"></script>

  <div class="container">
    <h1>发表文章页</h1>
    <hr>
    <form id="form">
      <!-- 在进入文章添加页面的一瞬间，就立即把 文章的 作者Id，保存到 一个隐藏域中，防止 session 失效的问题 -->
      <input type="hidden" name="authorId" value="<%= user.id %>">
      <div class="form-group">
        <label>文章标题：</label>
        <input type="text" name="title" class="form-control" required>
      </div>

      <div class="form-group">
        <label>文章内容：</label>
        <textarea name="content" class="form-control" id="editor"></textarea>
      </div>

      <div class="form-group">
        <input type="submit" value="发表文章" class="btn btn-primary">
      </div>
    </form>
  </div>

  <script>
    $(function () {
      // 文本编辑器
      let mditor = Mditor.fromTextarea(document.getElementById('editor'));
      // 点击发送
      $('#form').on('submit',function (e) {
        // 阻止跳转
        e.preventDefault();
        // 发送ajax 
        $.ajax({
          type:'post',
          url:'/article/add',
          data:$('#form').serialize(),
          success:(res) => {
            // console.log(res);
            if(res.status == 200) {
              location.href = '/article/info/' + res.insertId;
            }
          }
        })
      })
    })
  </script>

  <%- include('../layout/footer.ejs') %>